package com.yq.demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yq.demo.dao.UserJpaRepository;
import com.yq.demo.entity.User;
import com.yq.demo.service.UserService;

@Controller    // This means that this class is a Controller
@RequestMapping(path="/user") // This means URL's start with /demo (after Application path)
public class UserController {
    @Autowired // This means to get the bean called userRepository
               // Which is auto-generated by Spring, we will use it to handle the data
    private UserJpaRepository userRepository;

    @Autowired
    private UserService userService;

    /*@GetMapping(path="/add") // Map ONLY GET Requests
    public @ResponseBody String addNewUser(@RequestParam String name
            , @RequestParam String email) {
        // @ResponseBody means the returned String is the response, not a view name
        // @RequestParam means it is a parameter from the GET or POST request

        User user = new User();
        user.setUsername("userName");
        user.setFullname("fullName");
        user.setEmail(email);
        user.setActive(1);
        user.setUserType(1);

        userRepository.save(user);
        return "Saved";
    }*/

    @PostMapping(path="/add") // Map ONLY POST Requests
    public String addNewUser(@RequestParam String userName,@RequestParam String password,
            @RequestParam String fullName, @RequestParam String email,
            @RequestParam String usertype, @RequestParam String dateformat,
            @RequestParam(value = "timeforamt", defaultValue = "HH:mm:ss") String timeforamt,
            @RequestParam(value = "timezone", defaultValue = "GMT+8") String timezone,
            @RequestParam String language, Model model) {
        // String means the returned String is the thymeleaf template,  a view name
        // @RequestParam means it is a parameter from the GET or POST request

        User user = new User();
        user.setUsername(userName);
        user.setFullname(fullName);
        user.setEmail(email);
        user.setLanguage(language);
        user.setPassword(password);
        user.setActive(1);
        user.setUserType(1);
        user.setCan_delete(1);
        user.setTimeZone(timezone);
        user.setTimeFormat(timeforamt);
        user.setDateFormat(dateformat);
        userRepository.save(user);

        return "redirect:/user/all";
    }

    @GetMapping(path="/find") // Map ONLY GET Requests
    public @ResponseBody User findByName (@RequestParam String name) {
        // @ResponseBody means the returned String is the response, not a view name
        // @RequestParam means it is a parameter from the GET or POST request
        return userRepository.getByUserName(name);
    }


    /*@GetMapping(path="/all")
    public @ResponseBody Iterable<User> getAllUsers() {
        // This returns a JSON or XML with the users
        return userRepository.findAll();
    }
    */

    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public String getAllUsers(Model model) {
        List<User> userList = userRepository.findAll();
        model.addAttribute("userList", userList);


        String[] timezoneList = TimeZone.getAvailableIDs();
        model.addAttribute("timezoneList", timezoneList);

        return "admin/user/users";
    }

    @GetMapping(path="/init")
    public @ResponseBody Iterable<User> insertInitialUsers() {
        // This returns a JSON or XML with the users
        User user = new User();
        user.setUsername("张三");
        user.setEmail("zhangsan@qq.com");
        user.setLanguage("zh_CN");
        user.setPassword("password");
        user.setActive(1);
        user.setUserType(1);
        user.setCan_delete(1);
        userRepository.save(user);

        user = new User();
        user.setUsername("李四");
        user.setPassword("password");
        user.setEmail("lisi@qq.com");
        user.setLanguage("zh_CN");
        user.setActive(1);
        user.setUserType(1);
        user.setCan_delete(1);
        userRepository.save(user);

        user = new User();
        user.setUsername("王五");
        user.setPassword("password");
        user.setLanguage("zh_CN");
        user.setEmail("wangwu@qq.com");
        user.setActive(1);
        user.setUserType(1);
        user.setCan_delete(1);
        userRepository.save(user);

        return userRepository.findAll();
    }


    @GetMapping(path="/pages")
    public @ResponseBody Page<User> pagingUsers(@RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber,
            @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize) {
        Sort sort = new Sort(Direction.DESC, "id");
        Pageable pageable = new PageRequest(pageNumber, pageSize, sort);

        Page<User> pageUsers = userRepository.findAll(pageable);
        return pageUsers;
    }

    @GetMapping(path="/delete/{id}")
    public @ResponseBody String deleteUser(@PathVariable Long id) {
         userRepository.delete(id);
         return  "delete one";
    }

    @RequestMapping("/findByName/{name}")
    @ResponseBody
    public Map<String,Object> getUserByName(@PathVariable String name){
        Map<String,Object> result = new HashMap<String, Object>();
        User user = userService.getUserByName(name);
        if (user != null) {
            result.put("name", user.getUsername());
            result.put("Email", user.getEmail());
            result.put("Type",user.getUserType());
        }
        return result;
    }

}